﻿@using AntDesign
@using lcwb.Shared.Service
@inject NoteDrawerService DrawerService
<Form Model="@DrawerService.CurrentNote" Layout="@FormLayout.Vertical">
    @if (DrawerService.IsBasicStep)
    {
        <FormItem Label="标题"
        Rules="@(new[] { new FormValidationRule { Required = true, Message = "标题不能为空" } })">
            <Input @bind-Value="DrawerService.CurrentNote.Title" />
        </FormItem>
        <Button Type="ButtonType.Primary" OnClick="HandleNext">下一步</Button>
    }
    else
    {
        <FormItem Label="内容">
            <TextArea @bind-Value="DrawerService.CurrentNote.Content" Rows="8" />
        </FormItem>
        <Button Type="ButtonType.Primary" OnClick="HandleSubmit" disabled="@IsButtonDisabled">完成</Button>
    }
</Form>

@code{
    private bool IsButtonDisabled { get; set; } = false;
    protected override async Task OnInitializedAsync()
    {
        DrawerService.OnChange += StateHasChanged;
        // 检查是否有有效的 JWT
        // 可以略去
        await Task.Delay(1);
        base.OnInitialized();
    }

    public void Dispose()
    {
        DrawerService.OnChange -= StateHasChanged;
    }

    private void HandleNext()
    {
        if (!string.IsNullOrEmpty(DrawerService.CurrentNote.Title))
            DrawerService.NextStep();
    }
    private async Task HandleSubmit()
    {
        //DrawerService.CurrentNote.UserId = 1;
        //DrawerService.CurrentNote.CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds();
        IsButtonDisabled = true;
        await DrawerService.NoteFinished();
        DrawerService.ResetStep();
        await Task.Delay(100);
        IsButtonDisabled = false;
    }
}